#ifndef LJSTACK_H
#define LJSTACK_H

#include "LJList.h"

/*
 * A Stack based on LJList
 */
template<typename T>
class LJStack
{
	LJList<T> *m_pList;

	// create
	void Create();
	// delete
	void Delete();
public:
	LJStack();

	~LJStack(void);
	// push
	bool Push(const T& data);
	// pop
	bool Pop(T& data);
	// top
	bool Top(T& data) const;
	// Empty
	bool Empty() const;
};

template<typename T>
LJStack<T>::LJStack()
	:m_pList(NULL)
{
	Create();
}

template<typename T>
LJStack<T>::~LJStack(void)
{
	Delete();
}

template<typename T>
void LJStack<T>::Create()
{
	m_pList = new LJList<T>();
}

template<typename T>
void LJStack<T>::Delete()
{
	if(m_pList)
	{
		delete m_pList;
	}
}

template<typename T>
bool LJStack<T>::Push(const T& data)
{
	return m_pList->Insert(NULL, data);
}

template<typename T>
bool LJStack<T>::Pop(T& data)
{
	LJList<T>::iterator it = m_pList->begin();
	data = (*it)->Data();
	return m_pList->Delete(m_pList->Head());
}

template<typename T>
bool LJStack<T>::Top(T& data) const
{
	LJNode<T> *top = m_pList->Head();
	if(top)
	{
		data = top->Data();
		return true;
	}
	return false;
}

template<typename T>
bool LJStack<T>::Empty() const
{
	return m_pList->Size() == 0;
}
#endif